package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zsk
 * @create 2022-02-16-17:57
 */
@RestController
public class SetmealController {

    //依赖注入
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private DishService dishService;

    /*
    查询所有
    Request URL: http://localhost:8080/setmeal/page?page=1&pageSize=10
    Request Method: GET
     */
    @GetMapping("/setmeal/page")
    public ResultInfo findByPage(@RequestParam(value = "page",defaultValue = "1") Integer pageNum,//代表当前页数，
                                 @RequestParam(defaultValue = "10") Integer pageSize,//代表一页多少条数据
                                 String name){//代表可能按用户姓名模糊查询

        //调用service层查询,返回page集合对象
        Page<Setmeal> page = setmealService.findByPage(pageNum,pageSize,name);

        //返回结果
        return ResultInfo.success(page);
    }


    /*
    根据菜品分类id查询菜品列表
    Request URL: http://localhost:8080/dish/list?categoryId=1397844263642378242
    Request Method: GET
     */
    @GetMapping("/dish/list")
    public ResultInfo findByCategoryId(Long categoryId,String name){

        //根据菜单表查询菜品分类
        List<Dish> dishList = dishService.findByCategoryId(categoryId,name);
        //返回结果
        return ResultInfo.success(dishList);
    }

    /*
    修改前回显套餐数据
    Request URL: http://localhost:8080/setmeal/1494239075477213186
    Request Method: GET
 */
    @GetMapping("/setmeal/{id}")
    public ResultInfo updateById(@PathVariable("id") long id){

        //调用service层修改
        Setmeal setmeal = setmealService.updateById(id);

        //返回结果
        return ResultInfo.success(setmeal);
    }


    //==============下列操作都会对数据库产生影响，为了包子Redis中数据的更新，
    // 需要操作数据库的同时,删除Redis中得缓存数据==================\\

    /*
    添加套餐数据
    Request URL: http://localhost:8080/setmeal
    Request Method: POST
     */
    @CacheEvict(value = "setmeal",key = "#setmeal.categoryId")
    @PostMapping("/setmeal")
    public ResultInfo save(@RequestBody Setmeal setmeal){

        //调用service层添加
        setmealService.save(setmeal);

        //返回结果
        return ResultInfo.success(null);
    }

    /*
    删除套餐
    Request URL: http://localhost:8080/setmeal?ids=1494232798831292418
    Request Method: DELETE
     */
    @CacheEvict(value = "setmeal",allEntries = true)//删除setmeal下的所有数据
    @DeleteMapping("/setmeal")
    public ResultInfo deleteById(Long[] ids){
        //判断
        if (ids != null && ids.length > 0){
            //调用service查询
            setmealService.deleteById(ids);
        }
        //返回结果
        return ResultInfo.success(null);
    }

    /*
    修改状态
    Request URL: http://localhost:8080/setmeal/status/0?ids=1494232798831292418
    Request Method: POST
     */
    @CacheEvict(value = "setmeal",allEntries = true)
    @PostMapping("/setmeal/status/{status}")
    public ResultInfo updateStatus(@PathVariable("status") Integer status,
                                   @RequestParam List<Long> ids){
        //调用service层修改
        setmealService.updateStatus(status,ids);
        //返回结果
        return ResultInfo.success(null);
    }


    /*
    修改数据
    Request URL: http://localhost:8080/setmeal
    Request Method: PUT
     */
    @CacheEvict(value = "setmeal",allEntries = true)
    @PutMapping("/setmeal")
    public ResultInfo update(@RequestBody Setmeal setmeal){

        //调用service修改
        setmealService.update(setmeal);
        //返回结果
        return ResultInfo.success(null);
    }
}
